package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.ActivityJoinLists;
import com.xjc.business.entity.domain.AllStudentStayRelations;
import com.xjc.business.entity.po.stu.ActivityJoinPo;
import com.xjc.business.entity.vo.stu.ActivityCommentsVo;
import com.xjc.business.entity.vo.stu.ActivityCurrentJoinVo;
import com.xjc.business.entity.vo.stu.ActivityJoinVo;
import com.xjc.business.entity.vo.ActivityRankVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.ActivityJoinListsMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【activity_join_lists(宿舍评优活动参加表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:30
*/
@Service
public class ActivityJoinListsServiceImpl extends ServiceImpl<ActivityJoinListsMapper, ActivityJoinLists>
    implements ActivityJoinListsService{

    @Autowired
    @Lazy
    DormitoriesService dormitoriesService;

    @Autowired
    @Lazy
    StudentsService studentsService;

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    ActivityCommentsService activityCommentsService;

    /**
     * 获取宿舍评优活动参与列表-学生端
     * @param id 宿舍评优id
     * @return Result
     */
    @Override
    public Result<List<ActivityJoinVo>> getActivityJoinListAtStu(Integer id) {
        LambdaQueryWrapper<ActivityJoinLists> activityJoinListsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getActivityId, id);
        List<ActivityJoinVo> activityJoinVoList = list(activityJoinListsLambdaQueryWrapper).stream().map(activityJoinLists -> {
            ActivityJoinVo activityJoinVo = new ActivityJoinVo();

            activityJoinVo.setId(activityJoinLists.getId());
            activityJoinVo.setDormNo(dormitoriesService.getById(activityJoinLists.getDormId()).getDno()+"");
            activityJoinVo.setPublishStuName(studentsService.getById(activityJoinLists.getPublishId()).getName());
            activityJoinVo.setReason(activityJoinLists.getReason());
            String pics = activityJoinLists.getPics();
            String videoSrcs = activityJoinLists.getVideoSrcs();
            if (pics!=null){
                String[] split = pics.split(";");
                activityJoinVo.setPicList(Arrays.asList(split));
            }else {
                activityJoinVo.setPicList(new ArrayList<>());
            }

            if (videoSrcs!=null){
                String[] split = videoSrcs.split(";");
                activityJoinVo.setVideoList(Arrays.asList(split));
            }else {
                activityJoinVo.setVideoList(new ArrayList<>());
            }
            activityJoinVo.setLikeNum(activityJoinLists.getLikeNum());
            activityJoinVo.setUnlikeNum(activityJoinLists.getUnlikeNum());
            activityJoinVo.setCanCommit(activityJoinLists.getCanCommit());
            activityJoinVo.setCreateTime(activityJoinLists.getCreateTime());
            return activityJoinVo;
        }).collect(Collectors.toList());
        return Result.okWithData(activityJoinVoList);
    }

    /**
     * 获取宿舍评优活动排名列表-前十
     * @param id 宿舍评优id
     * @return Result
     */
    @Override
    public Result<List<ActivityRankVo>> getActivityRankList(Integer id) {
        LambdaQueryWrapper<ActivityJoinLists> activityJoinListsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getActivityId, id);
        activityJoinListsLambdaQueryWrapper.orderByDesc(ActivityJoinLists::getLikeNum);
        Page<ActivityJoinLists> activityJoinListsPage = new Page<>(1, 10);

        page(activityJoinListsPage, activityJoinListsLambdaQueryWrapper);
        List<ActivityJoinLists> activityJoinListsPageRecords = activityJoinListsPage.getRecords();
        List<ActivityRankVo> activityRankVoList = activityJoinListsPageRecords.stream().map(activityJoinLists -> {
            ActivityRankVo activityRankVo = new ActivityRankVo();
            activityRankVo.setDormNo(dormitoriesService.getById(activityJoinLists.getDormId()).getDno()+"");
            activityRankVo.setLikeNum(activityJoinLists.getLikeNum());
            activityRankVo.setUnlikeNum(activityJoinLists.getUnlikeNum());
            return activityRankVo;
        }).collect(Collectors.toList());

        return Result.okWithData(activityRankVoList);
    }

    /**
     * 参加宿舍评优活动
     * @param activityJoinPo 信息
     * @return Result
     */
    @Override
    public Result<String> joinActivity(ActivityJoinPo activityJoinPo) {
        Integer stuId = wxUserService.getById(activityJoinPo.getWxId()).getStuId();
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        Integer dormId = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper).getDormId();

        LambdaQueryWrapper<ActivityJoinLists> activityJoinListsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getActivityId,activityJoinPo.getActivityId());
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getDormId,dormId);
        if (getOne(activityJoinListsLambdaQueryWrapper)!=null){
            return Result.ok("所在宿舍已经参加此活动");
        }

        ActivityJoinLists activityJoinLists = new ActivityJoinLists();
        activityJoinLists.setActivityId(activityJoinPo.getActivityId());
        activityJoinLists.setDormId(dormId);
        activityJoinLists.setPublishId(stuId);
        activityJoinLists.setReason(activityJoinPo.getReason());
        List<String> picList = activityJoinPo.getPicList();
        List<String> videoList = activityJoinPo.getVideoList();
        if (picList.size() > 0){
            activityJoinLists.setPics(String.join(";",picList));
        }
        if (videoList.size() > 0){
            activityJoinLists.setVideoSrcs(String.join(";",videoList));
        }
        activityJoinLists.setLikeNum(0);
        activityJoinLists.setUnlikeNum(0);
        activityJoinLists.setCanCommit(activityJoinPo.getCanCommit());
        activityJoinLists.setCreateTime(new Date());
        activityJoinLists.setUpdateTime(new Date());
        save(activityJoinLists);
        return Result.okWithData("参加成功");
    }

    /**
     * 判断是否已经参加
     * @param activityId 宿舍评优活动id
     * @param wxId       微信id
     * @return Result
     */
    @Override
    public Result<Boolean> hasJoined(Integer activityId, Integer wxId) {
        Integer stuId = wxUserService.getById(wxId).getStuId();
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        Integer dormId = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper).getDormId();

        LambdaQueryWrapper<ActivityJoinLists> activityJoinListsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getActivityId,activityId);
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getDormId,dormId);
        if (getOne(activityJoinListsLambdaQueryWrapper)!=null){
            return Result.okWithData(true);
        }

        return Result.okWithData(false);
    }

    /**
     * 获取当前参加信息
     * @param activityId 宿舍评优活动id
     * @param wxId       微信id
     * @return Result
     */
    @Override
    public Result<ActivityCurrentJoinVo> getCurrentJoinInfo(Integer activityId, Integer wxId) {
        Integer stuId = wxUserService.getById(wxId).getStuId();
        LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId,stuId);
        Integer dormId = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper).getDormId();

        LambdaQueryWrapper<ActivityJoinLists> activityJoinListsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getActivityId,activityId);
        activityJoinListsLambdaQueryWrapper.eq(ActivityJoinLists::getDormId,dormId);

        ActivityJoinLists joinLists = getOne(activityJoinListsLambdaQueryWrapper);
        if (joinLists!=null){

            ActivityCurrentJoinVo activityCurrentJoinVo = new ActivityCurrentJoinVo();
            activityCurrentJoinVo.setReason(joinLists.getReason());
            String pics = joinLists.getPics();
            String videoSrcs = joinLists.getVideoSrcs();
            if (pics!=null){
                activityCurrentJoinVo.setPicList(Arrays.asList(pics.split(";")));
            }
            if (videoSrcs!=null){
                activityCurrentJoinVo.setVideoList(Arrays.asList(videoSrcs.split(";")));
            }
            activityCurrentJoinVo.setPublishStuName(studentsService.getById(joinLists.getPublishId()).getName());
            activityCurrentJoinVo.setDormNo(dormitoriesService.getById(dormId).getDno()+"");
            activityCurrentJoinVo.setCanCommit(joinLists.getCanCommit());
            activityCurrentJoinVo.setLikeNum(joinLists.getLikeNum());
            activityCurrentJoinVo.setUnlikeNum(joinLists.getUnlikeNum());
            activityCurrentJoinVo.setCreateTime(joinLists.getCreateTime());

            Integer id = joinLists.getId();
            List<ActivityCommentsVo> activityCommentsVos = activityCommentsService.getActivityCommentsList(id).getData();
            activityCurrentJoinVo.setCommentsList(activityCommentsVos);

            return Result.okWithData(activityCurrentJoinVo);
        }else {
            return Result.okWithData(null);
        }
    }
}




